package com.xg.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xg.sys.dto.SysDictionaryDTO;
import com.xg.sys.entity.SysDictionary;
import com.xg.sys.mapper.SysDictionaryMapper;
import com.xg.sys.service.SysDictionaryService;
import com.xg.frame.constant.SysConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ChenLiang
 * @since 2019-04-18
 */
@Slf4j
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements SysDictionaryService {

    @Autowired
    SysDictionaryService sysDictionaryService;

    @Override
    public void page(Page<SysDictionary> page, String classCode, String className) throws Exception {
        super.page(page,
                new LambdaQueryWrapper<SysDictionary>()
                        .like(!StringUtils.isEmpty(classCode), SysDictionary::getClassCode, classCode)
                        .like(!StringUtils.isEmpty(className), SysDictionary::getClassName, className)
                        .orderByAsc(SysDictionary::getDataCode)
        );
    }

    @Override
    @Cacheable(value = {SysConstant.CATCHE_SYS_DIC}, key = "#classCode")
    public List<SysDictionary> getDiclist(String classCode) {
        log.info(classCode + "查询数据库");
        SysDictionary data = new SysDictionary();
        data.setClassCode(classCode);
        List<SysDictionary> list = baseMapper.selectList(new QueryWrapper<>(data));
        return list;
    }

    @Override
    public Optional<SysDictionary> getDicByDataCode(String classCode, String dataCode) {
        List<SysDictionary> diclist = sysDictionaryService.getDiclist(classCode);
        return diclist.stream()
                .filter(vo -> org.apache.commons.lang3.StringUtils.equals(dataCode, vo.getDataCode()))
                .findAny();
    }

    @Override
    @CacheEvict(value = {SysConstant.CATCHE_SYS_DIC}, key = "#data.classCode")
    public boolean update(SysDictionaryDTO data) {
        SysDictionary dictionary = new SysDictionary();
        BeanUtils.copyProperties(data, dictionary);
        return this.updateById(dictionary);
    }

    @Override
    @CacheEvict(value = {SysConstant.CATCHE_SYS_DIC}, key = "#data.classCode")
    public boolean insert(SysDictionaryDTO data) {
        SysDictionary dictionary = new SysDictionary();
        BeanUtils.copyProperties(data, dictionary);
        return this.save(dictionary);
    }

    @Override
    @CacheEvict(value = {SysConstant.CATCHE_SYS_DIC}, key = "#data.classCode")
    public boolean delete(SysDictionary data) {
        return this.removeById(data.getId());
    }

}
